package com.content.scaffold.controller.sys;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.content.scaffold.common.config.ClassImpl;
import com.content.scaffold.common.web.ResultBody;
import com.content.scaffold.model.dto.sys.QueryUserPage;
import com.content.scaffold.model.entity.SysUser;
import com.content.scaffold.plugins.spi.MessagePlugin;
import com.content.scaffold.service.sys.SysUserService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.Executor;

/**
 * 用户相关接口
 * @author xiaojl
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("sys")
public class UserController {

    final private SysUserService sysUserService;

    final private Executor scaffoldTaskExecutor;
    final private ClassImpl classImpl;


    /**
     *  用户列表查询 （分页）
     * @param params 参数
     * @return 分页数据
     */
   @PostMapping("queryUserPage")

    public ResultBody<Page<SysUser>> queryUserPage(@RequestBody QueryUserPage params){
        Page<SysUser> page =    sysUserService.queryUserPage(params);
        return ResultBody.ok(page);

    }

    /**
     *  添加后修改用户
     * @param user 用户信息
     * @return 用户信息
     */
    @PostMapping("saveUser")
    public ResultBody<SysUser> saveUser(@RequestBody SysUser user){
        SysUser sysUser =  sysUserService.saveUser(user);
       return ResultBody.ok(sysUser);
    }

    @GetMapping(value = "getUser")
    public ResultBody<SysUser> getUser(@RequestParam String userId){
        SysUser sysUser = sysUserService.getById(userId);
        return ResultBody.ok(sysUser);
    }

    @PostConstruct
    public void logExecutorType() {
        System.out.println("Current TaskExecutor: " + scaffoldTaskExecutor.getClass().getName());
    }

    @GetMapping("/sendMsg")
    public String sendMsg() throws Exception{
        for (int i = 0; i < classImpl.getClazz().length; i++) {
            Class<?> pluginClass = Class.forName(classImpl.getClazz()[i]);
            MessagePlugin messagePlugin = (MessagePlugin) pluginClass.getDeclaredConstructor().newInstance();
             messagePlugin.sendMsg(new JSONObject());
        }
        return "success";
    }

    @PostMapping("/queryPage")
    public ResultBody<Page<SysUser>> queryPage(@RequestBody QueryUserPage params){
        Page<SysUser> page =    sysUserService.queryUserPage(params);
        return ResultBody.ok(page);
    }
    /**
     *  查询列表
     * @param params 列表参数
     * @return 列表数据
     */
    @PostMapping("/queryList")
    public ResultBody<List<SysUser>> queryList(@RequestBody SysUser params){
        List<SysUser> page =    sysUserService.queryList(params);
        return ResultBody.ok(page);
    }


}
